<template>
  <div class="doctor-profile-panel">
    <!-- 主要内容 -->
    <div class="profile-container">
      <!-- 标题区域 -->
      <div class="header-section">
        <div class="title-wrapper">
          <el-icon size="28" class="title-icon"><UserFilled /></el-icon>
          <div class="title-content">
            <h1 class="page-title">完善个人信息</h1>
            <p class="page-subtitle">请填写您的专业信息，以便为患者提供更好的服务</p>
          </div>
        </div>
      </div>
      
      <!-- 表单区域 -->
      <div class="form-section">
        <div class="form-container">
          <el-form
            ref="profileFormRef"
            :model="profileForm"
            :rules="profileRules"
            size="large"
            label-position="top"
            class="profile-form"
          >
            <!-- 基本信息 -->
            <div class="form-group">
              <h3 class="group-title">
                <el-icon><User /></el-icon>
                基本信息
              </h3>
              <div class="form-row">
                <el-form-item label="年龄" prop="age" class="form-item-half">
                  <el-input-number
                    v-model="profileForm.age"
                    :min="25"
                    :max="70"
                    placeholder="请输入年龄"
                    class="form-input"
                  />
                </el-form-item>
                
                <el-form-item label="性别" prop="gender" class="form-item-half">
                  <el-select
                    v-model="profileForm.gender"
                    placeholder="请选择性别"
                    class="form-input"
                  >
                    <el-option label="男" :value="1" />
                    <el-option label="女" :value="2" />
                  </el-select>
                </el-form-item>
              </div>
              
              <el-form-item label="手机号码" prop="phone">
                <el-input
                  v-model="profileForm.phone"
                  placeholder="请输入您的手机号码"
                  prefix-icon="Phone"
                  clearable
                  class="form-input"
                />
              </el-form-item>
              
              <el-form-item label="邮箱地址" prop="email">
                <el-input
                  v-model="profileForm.email"
                  placeholder="请输入您的邮箱地址"
                  prefix-icon="Message"
                  clearable
                  class="form-input"
                />
              </el-form-item>
            </div>
            
            <!-- 专业信息 -->
            <div class="form-group">
              <h3 class="group-title">
                <el-icon><Star /></el-icon>
                专业信息
              </h3>
              
              <el-form-item label="医生工号" prop="doctorNo">
                <el-input
                  v-model="profileForm.doctorNo"
                  placeholder="请输入您的医生工号"
                  prefix-icon="Notebook"
                  clearable
                  class="form-input"
                />
              </el-form-item>
              
              <el-form-item label="医师资格证书编号" prop="qualificationNo">
                <el-input
                  v-model="profileForm.qualificationNo"
                  placeholder="请输入医师资格证书编号"
                  prefix-icon="Document"
                  clearable
                  class="form-input"
                />
              </el-form-item>
              
              <el-form-item label="身份证号码" prop="idCard">
                <el-input
                  v-model="profileForm.idCard"
                  placeholder="请输入18位身份证号码"
                  prefix-icon="CreditCard"
                  clearable
                  class="form-input"
                />
              </el-form-item>
            </div>
            
            <!-- 按钮区域 -->
            <div class="button-section">
              <el-button
                size="large"
                @click="handleCancel"
                class="cancel-btn"
              >
                跳过填写
              </el-button>
              <el-button
                type="primary"
                size="large"
                @click="handleSubmit"
                :loading="loading"
                class="submit-btn"
              >
                <el-icon v-if="!loading"><Check /></el-icon>
                {{ loading ? '保存中...' : '保存信息' }}
              </el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  UserFilled, User, Star, Check, Phone, Message, 
  Notebook, Document, CreditCard
} from '@element-plus/icons-vue'
import { updateDoctorProfile } from '../api/doctor.js'

const router = useRouter()
const loading = ref(false)
const profileFormRef = ref(null)

// 个人信息表单数据
const profileForm = reactive({
  age: null,
  gender: null,
  phone: '',
  email: '',
  doctorNo: '',
  qualificationNo: '',
  idCard: ''
})

// 手机号验证器
const validatePhone = (rule, value, callback) => {
  if (value && !/^1[3-9]\d{9}$/.test(value)) {
    callback(new Error('请输入正确的手机号码'))
  } else {
    callback()
  }
}

// 邮箱验证器
const validateEmail = (rule, value, callback) => {
  if (value && !/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/.test(value)) {
    callback(new Error('请输入正确的邮箱地址'))
  } else {
    callback()
  }
}

// 表单验证规则
const profileRules = {
  age: [
    { required: true, message: '请输入年龄', trigger: 'blur' },
    { type: 'number', min: 25, max: 70, message: '年龄必须在25-70岁之间', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { validator: validatePhone, trigger: 'blur' }
  ],
  email: [
    { validator: validateEmail, trigger: 'blur' }
  ],
  doctorNo: [
    { required: true, message: '请输入医生工号', trigger: 'blur' },
    { min: 3, max: 20, message: '工号长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  qualificationNo: [
    { required: true, message: '请输入医师资格证书编号', trigger: 'blur' },
    { min: 10, max: 30, message: '证书编号长度在 10 到 30 个字符', trigger: 'blur' }
  ],
  idCard: [
    { required: true, message: '请输入身份证号码', trigger: 'blur' },
    { pattern: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: '请输入正确的18位身份证号码', trigger: 'blur' }
  ]
}

// 获取临时存储的医生信息
let doctorInfo = null
onMounted(() => {
  const tempInfo = localStorage.getItem('tempDoctorInfo')
  if (tempInfo) {
    doctorInfo = JSON.parse(tempInfo)
  } else {
    // 如果没有临时信息，说明不是从注册页面跳转过来的，重定向到注册页面
    ElMessage.warning('请先完成注册')
    router.push('/doctor/register')
  }
})

// 提交表单
const handleSubmit = async () => {
  if (!profileFormRef.value || !doctorInfo) return
  
  try {
    const valid = await profileFormRef.value.validate()
    if (!valid) return
    
    loading.value = true
    
    // 更新个人信息
    await updateDoctorProfile(doctorInfo.doctorId, profileForm)
    
    ElMessage.success({
      message: '个人信息保存成功！正在跳转到登录页面...',
      duration: 2500
    })
    
    // 清除临时存储的信息
    localStorage.removeItem('tempDoctorInfo')
    
    // 跳转到主页面
    setTimeout(() => {
      router.push('/dashboard')
    }, 2000)
    
  } catch (error) {
    console.error('Update profile error:', error)
    
    ElMessage.error({
      message: error.message || '保存失败，请检查输入信息',
      duration: 3000
    })
  } finally {
    loading.value = false
  }
}


// 跳过填写
const handleCancel = () => {
  ElMessage({
    message: '您可以稍后在个人中心完善信息',
    type: 'info',
    duration: 2000
  })
  
  // 清除临时存储的信息
  localStorage.removeItem('tempDoctorInfo')
  
  // 跳转到主页面
  setTimeout(() => {
    router.push('/dashboard')
  }, 1000)
}
</script>

<style scoped>
.doctor-profile-panel {
  width: 100%;
  height: 100%;
  padding: 20px;
  background: transparent;
  overflow-y: auto;
  box-sizing: border-box;
}

.profile-container {
  width: 100%;
  max-width: none;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  border: 1px solid #e0e0e0;
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 120px);
}

.header-section {
  padding: 30px 40px 20px 40px;
  background: linear-gradient(135deg, rgba(103, 58, 183, 0.05) 0%, rgba(0, 188, 212, 0.05) 100%);
  border-bottom: 1px solid rgba(103, 58, 183, 0.1);
}

.title-wrapper {
  display: flex;
  align-items: center;
  gap: 15px;
}

.title-icon {
  color: #673AB7;
  background: linear-gradient(135deg, rgba(103, 58, 183, 0.1) 0%, rgba(0, 188, 212, 0.1) 100%);
  padding: 12px;
  border-radius: 12px;
  box-shadow: 0 3px 10px rgba(103, 58, 183, 0.2);
}

.title-content {
  flex: 1;
}

.page-title {
  font-size: 24px;
  font-weight: 700;
  color: #333;
  margin: 0 0 6px 0;
  background: linear-gradient(135deg, #673AB7 0%, #00BCD4 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.page-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
  opacity: 0.8;
}

.form-section {
  flex: 1;
  padding: 20px 40px 30px 40px;
  overflow-y: auto;
  width: 100%;
}

.form-container {
  width: 100%;
  max-width: none;
  margin: 0;
}

.profile-form {
  width: 100%;
}

.form-group {
  margin-bottom: 30px;
  padding: 25px 30px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.8) 0%, rgba(248, 250, 255, 0.6) 100%);
  border-radius: 16px;
  box-shadow: 
    0 4px 16px rgba(0, 0, 0, 0.06),
    inset 0 1px 0 rgba(255, 255, 255, 0.5);
  border: 1px solid rgba(103, 58, 183, 0.08);
}

.group-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 600;
  color: #673AB7;
  margin: 0 0 20px 0;
  padding-bottom: 12px;
  border-bottom: 2px solid rgba(103, 58, 183, 0.1);
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 30px;
}

.form-item-half {
  margin-bottom: 20px;
}

.form-input {
  width: 100%;
  border-radius: 12px;
  border: 2px solid #f0f0f0;
  transition: all 0.3s ease;
}

.form-input:hover {
  border-color: #673AB7;
}

.form-input.is-focus {
  border-color: #673AB7;
  box-shadow: 0 0 0 3px rgba(103, 58, 183, 0.1);
}

.button-section {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  margin-top: 30px;
  padding-top: 30px;
  border-top: 1px solid rgba(103, 58, 183, 0.1);
}

.cancel-btn {
  flex: 1;
  max-width: 180px;
  height: 50px;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #f5f5f5 0%, #e0e0e0 100%);
  color: #666;
  border: 2px solid #e0e0e0;
  transition: all 0.3s ease;
}

.cancel-btn:hover {
  background: linear-gradient(135deg, #e0e0e0 0%, #d0d0d0 100%);
  color: #555;
  transform: translateY(-1px);
}

.submit-btn {
  flex: 2;
  height: 50px;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #673AB7 0%, #00BCD4 100%);
  border: none;
  box-shadow: 0 4px 15px rgba(103, 58, 183, 0.4);
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(103, 58, 183, 0.6);
}

/* 大屏幕优化 */
@media (min-width: 1200px) {
  .form-section {
    padding: 30px 60px 40px 60px;
  }
  
  .header-section {
    padding: 40px 60px 25px 60px;
  }
  
  .form-group {
    padding: 30px 40px;
  }
  
  .form-row {
    gap: 40px;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-section, .form-section {
    padding-left: 20px;
    padding-right: 20px;
  }
  
  .title-wrapper {
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }
  
  .form-row {
    grid-template-columns: 1fr;
    gap: 0;
  }
  
  .form-group {
    padding: 15px;
  }
  
  .button-section {
    flex-direction: column;
    gap: 15px;
  }
  
  .cancel-btn {
    max-width: none;
  }
}

@media (max-width: 480px) {
  .doctor-profile-panel {
    padding: 15px;
  }
  
  .header-section, .form-section {
    padding-left: 15px;
    padding-right: 15px;
  }
  
  .page-title {
    font-size: 20px;
  }
  
  .group-title {
    font-size: 16px;
  }
}
</style>
